/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package suit.ui;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * Extend this class to form your program. An {@link Application} is basically
 * the same as a regular MIDlet. The application must extend this class to allow
 * the application to control the application.
 * 
 * @author Devin Samarin
 * 
 */
public class Application extends MIDlet {

	protected Display display = Display.getDisplay(this);
	private Screen screen;

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	}

	protected void pauseApp() {
	}

	protected void startApp() throws MIDletStateChangeException {
		if (screen != null) {
			if (display.getCurrent() != screen)
				setScreen(screen);
		}
	}

	/**
	 * Sets the Displayable to be displayed. If the Displayable is a Screen, it
	 * will invalidate it resulting in everything being redrawn.
	 * 
	 * @param screen
	 *            the Displayable requested to be made current; null is allowed
	 */
	public void setScreen(Displayable screen) {
		boolean isscreen;
		if (this.screen != null)
			this.screen.realized = false;
		if (isscreen = screen instanceof Screen) {
			this.screen = (Screen) screen;
			((Screen) screen).app = this;
			((Screen) screen).display = display;
		}
		display.setCurrent(screen);
		if (isscreen) {
			((Screen) screen).realized = true;
			((Screen) screen).invalidate();
		}
	}

	/**
	 * Returns the last Screen object that was made current. This does not
	 * necessarily return the Displayable that was set with setScreen, since
	 * setScreen accepts any Displayable. If the Displayable was a Screen
	 * object, then that is returned.
	 * 
	 * @return the last Screen object that was made current; null if not set
	 */
	public Screen getScreen() {
		return screen;
	}

	/**
	 * Called when the pointer is pressed.
	 * <p>
	 * If true is returned, the event is passed down to the Screen's child.
	 * 
	 * @param x
	 *            the horizontal location where the pointer was pressed
	 *            (relative to the Screen)
	 * @param y
	 *            the vertical location where the pointer was pressed (relative
	 *            to the Screen)
	 * @return boolean representing whether or not to pass the event to the
	 *         current Screen's child
	 */
	public boolean onPointerPressed(int x, int y) {
		return true;
	}

	/**
	 * Called when the pointer is dragged.
	 * <p>
	 * If true is returned, the event is passed down to the Screen's child.
	 * 
	 * @param x
	 *            the horizontal location where the pointer was dragged to
	 *            (relative to the Screen)
	 * @param y
	 *            the vertical location where the pointer was dragged to
	 *            (relative to the Screen)
	 * @return boolean representing whether or not to pass the event to the
	 *         current Screen's child
	 */
	public boolean onPointerDragged(int x, int y) {
		return true;
	}

	/**
	 * Called when the pointer is released.
	 * <p>
	 * If true is returned, the event is passed down to the Screen's child.
	 * 
	 * @param x
	 *            the horizontal location where the pointer was released
	 *            (relative to the Screen)
	 * @param y
	 *            the vertical location where the pointer was released (relative
	 *            to the Screen)
	 * @return boolean representing whether or not to pass the event to the
	 *         current Screen's child
	 */
	public boolean onPointerReleased(int x, int y) {
		return true;
	}

	/**
	 * Called when a key is repeated.
	 * <p>
	 * If true is returned, the event is passed down to the Screen's child.
	 * 
	 * @param key
	 *            the key code of the key that was pressed
	 * @return boolean representing whether or not to pass the event to the
	 *         current Screen's child
	 */
	public boolean onKeyPressed(int key) {
		return true;
	}

	/**
	 * Called when a key is repeated.
	 * <p>
	 * If true is returned, the event is passed down to the Screen's child.
	 * 
	 * @param key
	 *            the key code of the key that was repeated
	 * @return boolean representing whether or not to pass the event to the
	 *         current Screen's child
	 */
	public boolean onKeyRepeated(int key) {
		return true;
	}

	/**
	 * Called when a key is released.
	 * <p>
	 * If true is returned, the event is passed down to the Screen's child.
	 * 
	 * @param key
	 *            the key code of the key that was released
	 * @return boolean representing whether or not to pass the event to the
	 *         current Screen's child
	 */
	public boolean onKeyReleased(int key) {
		return true;
	}

}
